package com.carkeeper.blog.controller;

import com.carkeeper.blog.dao.CategoryMapper;
import com.carkeeper.blog.pojo.Category;
import com.carkeeper.blog.service.CategoryService;
import com.carkeeper.entity.Result;
import com.carkeeper.entity.StatusCode;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.ws.rs.PUT;
import java.util.List;

/**
 * @program: carkeeper_parent
 * @Date: 2020/11/25 14:48
 * @Author: qgg
 * @Description:
 */
@RestController
@RequestMapping("/category")
@Api(tags = "分类管理接口api")
public class CategoryController {

    @Autowired
    public CategoryService categoryService;

    //增加
    @PostMapping("/add")
    @ApiOperation("新增分类接口")
    public Result add(@RequestBody Category category){
        categoryService.add(category);
        return new Result(true , StatusCode.OK , "添加成功");
    }

    //删除
    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除分类接口")
    public Result delete(@PathVariable("id") String id){
        categoryService.delete(id);
        return new Result(true , StatusCode.OK , "删除成功");
    }

    //修改
    @PutMapping("/update")
    @ApiOperation("修改分类接口")
    public Result update(@RequestBody Category category){
        categoryService.update(category);
        return new Result(true , StatusCode.OK , "修改成功");
    }

    //查询所有
    @GetMapping("/findAll")
    @ApiOperation("查询所有分类接口")
    public Result findAll(){
        List<Category> categoryList = categoryService.findAll();
        return new Result(true , StatusCode.OK , "查询成功" , categoryList);
    }

    //通过id查询内容
    @GetMapping("/findById/{id}")
    @ApiOperation("通过id查询分类接口")
    public Result findById(@PathVariable("id") String id){
        Category category = categoryService.findById(id);
        return new Result(true , StatusCode.OK , "查询成功" , category);
    }

    //分页查询
    @GetMapping("/{pageNum}/{pageSize}")
    @ApiOperation("分页查询分类接口")
    public Result findPage(@PathVariable("pageNum") Integer pageNum , @PathVariable("pageSize") Integer pageSize){
        PageInfo page = categoryService.findPage(pageNum, pageSize);
        return new Result(true , StatusCode.OK , "查询成功" , page);
    }
}


